/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package core.threads.server.communication;
import core.messages.*;
import core.threads.server.Server_Public_Data;
import core.threads.server.Server_Public_MsgBox;
import myTest.MYTest;
import java.io.IOException;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
import models.Player;

/**
 *
 * @author Invince
 */
public class T_Server_Sender extends Thread {
/*----------Attributs-------------------*/
    protected boolean stop;
    protected boolean suspend;

//    protected String clientAdd;
//    protected int clientPort;
//    protected Socket socket;
//    protected ObjectOutputStream objOutPut;


/*----------Public functions------------*/
    @Override
    public void run() {        
        try {
            /**-----Init-----**/
           MYTest.println("Thread:T_Server_Sender start");
            /**-----Loop----**/
            while(!this.stop){
                if(!this.suspend){ 
                    if(!Server_Public_MsgBox.msgBox_Server_Send_Msg.isEmpty()){
                        MYTest.println("Thread:T_Server_Sender get msg");
                        Msg msgSend = Server_Public_MsgBox.msgBox_Server_Send_Msg.poll();//从msgBox里取
                        send(msgSend);
                    }
                }
            }
           /**----End-----**///这里End部分写在下面的finally里了
        } catch (IOException ex) {
            Logger.getLogger(T_Server_Sender.class.getName()).log(Level.SEVERE, null, ex);
       }finally{
            try {
                synchronized(Server_Public_Data.semaGame){
                    for(Player sendTo : Server_Public_Data.game.getListPlayer().getList()){
                      sendTo.getConnection().getObjOutPut().close();//注意如果在send里open close的话，会把socket一起close掉
                      sendTo.getConnection().getSocket().close();
                    }
                }
            } catch (IOException ex) {
                Logger.getLogger(T_Server_Sender.class.getName()).log(Level.SEVERE, null, ex);
            }
       }

    }

    public void send(Object obj) throws IOException{
        if(!(obj instanceof Msg)){
            MYTest.println("Error send , waiting an instance of Msg!");
            return;
        }
        boolean isGameBegin;
        synchronized(Server_Public_Data.semaIsGameBegin){
            isGameBegin=Server_Public_Data.isGameBegin;
        }
        if(obj instanceof Msg_One){
            Msg_One tempMsg=(Msg_One) obj;
            String name=tempMsg.getReceiver();
            Player sendTo;            
            if(isGameBegin){//isGameBegin和game是共享变量
                synchronized(Server_Public_Data.semaGame){
                    sendTo=Server_Public_Data.game.getListPlayer().findPlayerByName(name);
                }
            }
            else{
                synchronized(Server_Public_Data.semaTempListPlayer){
                    sendTo=Server_Public_Data.tempListPlayer.findPlayerByName(name);
                }
            }
            if(!sendTo.equals(null)){
                sendTo.getConnection().getObjOutPut().writeObject(obj);
                sendTo.getConnection().getObjOutPut().flush();
                MYTest.println("A single Receiver message send.");
            }else{MYTest.println("Error send, player not found!");}
        }
        if(obj instanceof Msg_All){
            if(isGameBegin){
                synchronized(Server_Public_Data.semaGame){
                    if(Server_Public_Data.game.getListPlayer().size()!=0){
                        for(Player sendTo : Server_Public_Data.game.getListPlayer().getList()){
                            sendTo.getConnection().getObjOutPut().writeObject(obj);
                            sendTo.getConnection().getObjOutPut().flush();
                        }
                        MYTest.println("A To Everyone message send.");
                    }else{MYTest.println("Error send, player list is vide");}
                }
            }else{
                synchronized(Server_Public_Data.semaTempListPlayer){
                    if(Server_Public_Data.tempListPlayer.size()!=0){
                        for(Player sendTo : Server_Public_Data.tempListPlayer.getList()){
                            sendTo.getConnection().getObjOutPut().writeObject(obj);
                            sendTo.getConnection().getObjOutPut().flush();
                        }
                        MYTest.println("A To Everyone message send.");
                    }else{MYTest.println("Error send, player list is vide");}
                }
            }
        }
        
    }

    public void stopT(){
        synchronized(this){
            this.stop=true;
        }
    }

    public void suspendT(){
        synchronized(this){
            this.suspend=true;
        }
    }

    public void resumeT(){
        synchronized(this){
            this.suspend=false;
        }
    }
/*----------Private functions-----------*/
/*----------Constructor-----------------*/
    public T_Server_Sender() {
        synchronized(this){
            this.stop=false;
            this.suspend=false;
            MYTest.println("Thread:T_Server_Sender created");
        }
    }

/*----------Gets and sets---------------*/
}
